package leetcode;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0047.java
 * @Author : WangRuoyu
 * @Date 2023/11/3 20:03
 */


public class Q0047 {
    int[] nums;
    boolean[] st;
    List<Integer> tmp = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();

    public void dfs(int k) {
        if (k == nums.length) {
            res.add(new ArrayList<>(tmp));
            return;
        }
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (!st[i]) {
                if (set.contains(nums[i])) continue;
                set.add(nums[i]);
                st[i] = true;
                tmp.add(nums[i]);
                dfs(k + 1);
                st[i] = false;
                tmp.remove(tmp.size() - 1);
            }
        }
    }

    public List<List<Integer>> permuteUnique(int[] nums) {
        this.nums = nums;
        this.st = new boolean[nums.length];
        dfs(0);
        return res;
    }

    public static void main(String[] args) {
        Q0047 sl = new Q0047();
        int[] nums = {1, 2, 3};
        System.out.println(sl.permuteUnique(nums));
    }
}
